﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace IPMsgMoTest.ViewModels
{
    public class ViewManager : INotifyPropertyChanged
    {
        private static ViewManager instance = new ViewManager();

        public static ViewManager Instance
        {
            get
            {
                return instance;
            }
        }

        private ViewManager()
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    this.AllConversation = new ObservableCollection<ChatViewModel>();
                });
        }

        public ObservableCollection<ChatViewModel> AllConversation { get; private set; }

        internal void HandleRcvMsg(string hostname, string message)
        {
            ChatViewModel conversation = null;
            lock (this.AllConversation)
            {
                foreach (ChatViewModel chatview in this.AllConversation)
                {
                    if (chatview.Hostname == hostname)
                    {
                        conversation = chatview;
                        break;
                    }
                }
            }

            if (conversation == null)
            {
                conversation = new ChatViewModel();
                conversation.Hostname = hostname;

                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    this.AllConversation.Add(conversation);
                });
            }

            conversation.HandleRcvMsg(message);
        }

        internal void HandleSendMsg(string hostname, string message)
        {
            ChatViewModel conversation = null;
            lock (this.AllConversation)
            {
                foreach (ChatViewModel chatview in this.AllConversation)
                {
                    if (chatview.Hostname == hostname)
                    {
                        conversation = chatview;
                        break;
                    }
                }
            }

            if (conversation == null)
            {
                conversation = new ChatViewModel();
                conversation.Hostname = hostname;
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    this.AllConversation.Add(conversation);
                });
            }

            conversation.HandleSendMsg(message);
        }

        internal ChatViewModel GetChatViewModel(string hostname)
        {
            ChatViewModel conversation = null;
            lock (this.AllConversation)
            {
                foreach (ChatViewModel chatview in this.AllConversation)
                {
                    if (chatview.Hostname == hostname)
                    {
                        conversation = chatview;
                        break;
                    }
                }
            }

            if (conversation == null)
            {
                conversation = new ChatViewModel();
                conversation.Hostname = hostname;
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    this.AllConversation.Add(conversation);
                });
            }

            return conversation;
        }

        internal bool RemoveChatViewModel(string hostname)
        {
            lock (this.AllConversation)
            {
                foreach (ChatViewModel chatview in this.AllConversation)
                {
                    if (chatview.Hostname == hostname)
                    {
                        Deployment.Current.Dispatcher.BeginInvoke(() =>
                            {
                                this.AllConversation.Remove(chatview);
                            });

                        return true;
                    }
                }
            }

            return false;
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
